package model.service;

import model.Coordinate;
import model.Dimension;
import model.OnScreen;
import model.Snippet;
import model.location.WorldCoordinate;

public class Pixelator 
{
	private static double scaleX;
//	private static double scaleY;
	
	public static Snippet convertSnippet(String id, int x, int y, int z, int width, int height, boolean information, int displayType) 
	{
		WorldCoordinate worldCoord = new WorldCoordinate(x, y, z);
		Dimension worldDimension = new Dimension(width, height);
		
		Coordinate screenCoord = convertCoordinate(worldCoord);
		Dimension screenDimension = convertDimension(worldDimension);
		
		return new Snippet(id, screenCoord, screenDimension, information, displayType);
	}
	
	public static void calculateScales()
	{
		calculateScaleX();
		calculateScaleY();
	}
	
	private static void calculateScaleX()
	{
		double width = OnScreen.getInstance().getLowerRight().getX() -  OnScreen.getInstance().getUpperLeft().getX();
		
		scaleX = width / (double) OnScreen.getInstance().getScreenDimension().getWidth();
	}
	
	private static void calculateScaleY()
	{
//		double height = OnScreen.getInstance().getUpperLeft().getY() - OnScreen.getInstance().getLowerRight().getY();
		
//		scaleY = height / (double) OnScreen.getInstance().getScreenDimension().getHeight();
	}
	
	private static Dimension convertDimension(Dimension dimension)
    {
        int width = (int) (dimension.getWidth() / scaleX);
        int height = (int) (dimension.getHeight() / scaleX);
        Dimension dim = new Dimension(width, height);
        
        return dim;
    }

    private static Coordinate convertCoordinate(WorldCoordinate coordinate)
    {
        int xOffset = coordinate.getX() - OnScreen.getInstance().getUpperLeft().getX();
        int yOffset = OnScreen.getInstance().getUpperLeft().getY() - coordinate.getY();

        int xPixelOffset = (int) (xOffset / scaleX);
        int yPixelOffset = (int) (yOffset / scaleX);

        Coordinate coordinate2d = new Coordinate(xPixelOffset, yPixelOffset);

        return coordinate2d;
    }
    
    public static void calculateAngle()
    {
//    	int z = Math.abs(OnScreen.getInstance().getUpperLeft().getZ() - OnScreen.getInstance().getLowerRight().getZ());
//    	int x = Math.abs(OnScreen.getInstance().getUpperLeft().getX() - OnScreen.getInstance().getLowerRight().getX());
//    	
//    	double ratio = (double) z / (double) x;
//    	
//    	float angle = (float) Math.atan2(z, x);
//    	
//    	angle = (float) Math.toDegrees(angle);
//    	
//    	Log.d("ANGLE", String.format("depth: %d - width: %d - ratio: %f - angle: %f", z,x,(float) ratio,angle));
//    	
//    	OnScreen.getInstance().setAngle(angle);
    }
}
